package com.service.impl;

import com.dao.*;
import com.entity.Friend;
import com.entity.FriendVerifyMessage;
import com.entity.PostingInfo;
import com.service.FriendVerifyMessageService;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 好友验证业务实现
 *
 * @author lbb
 * @version 1.0
 * @date 2020.3.22
 */
@Service
public class FriendVerifyMessageServiceImpl implements FriendVerifyMessageService {
	//好友验证
	@Autowired
	private FriendVerifyMessageDao friendVerifyMessageDao;
	//好友
	@Autowired
	private FriendDao friendDao;
	//帖子
	@Autowired
	private PostingInfoDao postingInfoDao;
	//帖子权限
	@Autowired
	private PostingVisitPrivilegeDao postingVisitPrivilegeDao;
	@Autowired
	private UserInfoDao userInfoDao;

	//添加好友验证信息
	@Override
	public Boolean addFriendVerifyMessage(FriendVerifyMessage friendVerifyMessage) {
		if(friendVerifyMessageDao.getNotDisposeCount(friendVerifyMessage)>0){
			return false;
		}
		friendVerifyMessageDao.addFriendVerifyMessage(friendVerifyMessage);
		return true;
	}
	//获取用户的好友验证信息
	@Override
	public List<FriendVerifyMessage> getByToUserIdAllFriendVerifyMessage(Integer pageNum,Integer pageSize,Integer toUserId) {
		List<FriendVerifyMessage> results = friendVerifyMessageDao.getByToUserIdAllFriendVerifyMessage(pageNum, pageSize, toUserId);
		for (FriendVerifyMessage result : results) {
			result.setFromUserInfo(userInfoDao.getById(result.getFromUserId()));
			result.setToUserInfo(userInfoDao.getById(result.getToUserId()));
		}
		return results;
	}
	//状态
	@Override
	@Transactional
	public Boolean editStatus(FriendVerifyMessage friendVerifyMessage) {
		
		Integer userId = friendVerifyMessage.getToUserId();
		Integer friendId = friendVerifyMessage.getFromUserId();
		Integer friendTypeId = friendVerifyMessage.getFriendTypeId();
		Integer otherFriendTypeId = friendVerifyMessage.getOtherFriendTypeId();
		
		//同意添加好友
		if (friendVerifyMessage.getStatus().equals(1)) {
			//添加好友
			Friend friend = new Friend();
			friend.setUserId(userId);
			friend.setFriendId(friendId);
			friend.setTypeId(friendTypeId);
			friendDao.addFriend(friend);
			
			friend.setUserId(friendId);
			friend.setFriendId(userId);
			friend.setTypeId(otherFriendTypeId);
			friendDao.addFriend(friend);
			
			//当前用户全部的帖子ID
			List<Integer> userPostingIds = postingInfoDao.getUserPostingInfoAllId(userId);
			//好友的全部帖子ID
			List<Integer> friendPostingIds = postingInfoDao.getUserPostingInfoAllId(friendId);
			postingVisitPrivilegeDao.addPostingVisitPrivilegePostingIds(friendId,userPostingIds);
			postingVisitPrivilegeDao.addPostingVisitPrivilegePostingIds(userId,friendPostingIds);
		}
		
		return friendVerifyMessageDao.editStatus(friendVerifyMessage.getId(),friendVerifyMessage.getStatus());
	}
}
